#include "stdafx.h"

void drawtile_8x8_ddragon(u32 *dest,int p,int tilenum,u8 x,u8 y,u8 attrib,u8 *b,u32 *P)
{
u32 *s = dest + x + (y * p);
int i,j;
u8 c,b0,b1,b2,b3;

b += (tilenum & 0x3FF) * 32;	//8x8 tiles, 4bpp per tile
for(j=0;j<8;j++)
	{
	for(i=0;i<8;i++)
		{
		int offs = ((i >> 1) * 8) + j;
		
		c = b[offs];
		if((i & 1) == 0)
			{
			b0 = c & (1 << 0);
			b1 = c & (1 << 2);
			b2 = c & (1 << 4);
			b3 = c & (1 << 6);
			}
		else
			{
			b0 = c & (1 << 1);
			b1 = c & (1 << 3);
			b2 = c & (1 << 5);
			b3 = c & (1 << 7);
			}
		if(b0)	b0 = 1;
		if(b1)	b1 = 2;
		if(b2)	b2 = 4;
		if(b3)	b3 = 8;
		c = b0 | b1 | b2 | b3;
		if(c)
			s[i] = P[c | attrib];
		}
	s += p;
	}
}

void drawtile_16x16_ddragon(u32 *dest,int p,int tilenum,u8 x,u8 y,u8 attrib,u8 *b,u32 *P)
{

u32 *s = dest + x + (y * p);
int i,j;
u8 cc1,cc2;
u8 b0,b1,b2,b3;

b += tilenum * 64;		//16x16 tiles, 4bpp per tile
for(j=0;j<16;j++)
	{
	for(i=0;i<16;i++)
		{
		int offs = ((i >> 2) * 16) + j;
		
		cc1 = b[offs + 0x20000];
		cc2 = b[offs + 0x00000];
		switch(i & 3)
			{
			case 3:	//first group of four bits, offset = 3
				b3 = ((cc1 >> 3) & 1) << 0;
				b2 = ((cc1 >> 7) & 1) << 1;
				b1 = ((cc2 >> 3) & 1) << 2;
				b0 = ((cc2 >> 7) & 1) << 3;
				break;
			case 2:	//second group of four bits, offset = 2
				b3 = ((cc1 >> 2) & 1) << 0;
				b2 = ((cc1 >> 6) & 1) << 1;
				b1 = ((cc2 >> 2) & 1) << 2;
				b0 = ((cc2 >> 6) & 1) << 3;
				break;
			case 1:	//third group of four bits, offset = 1
				b3 = ((cc1 >> 1) & 1) << 0;
				b2 = ((cc1 >> 5) & 1) << 1;
				b1 = ((cc2 >> 1) & 1) << 2;
				b0 = ((cc2 >> 5) & 1) << 3;
				break;
			case 0:	//fourth group of four bits, offset = 0
				b3 = ((cc1 >> 0) & 1) << 0;
				b2 = ((cc1 >> 4) & 1) << 1;
				b1 = ((cc2 >> 0) & 1) << 2;
				b0 = ((cc2 >> 4) & 1) << 3;
				break;
			}
		s[i] = P[b0 | b1 | b2 | b3 | attrib];
		}
	s += p;
	}
}

void drawtile_16x16_ddragon_spr(u32 *dest,int p,int tilenum,u8 x,u8 y,u8 attrib,u8 *b,u32 *P)
{
u32 *s = dest + x + (y * p);
int i,j;
u8 cc1,cc2,c;
u8 b0,b1,b2,b3;

b += tilenum * 64;		//16x16 tiles, 4bpp per tile
for(j=0;j<16;j++)
	{
	for(i=0;i<16;i++)
		{
		int offs = ((i >> 2) * 16) + j;
		
		cc1 = b[offs + 0x40000];
		cc2 = b[offs + 0x00000];
		switch(i & 3)
			{
			case 3:	//first group of four bits, offset = 3
				b3 = ((cc1 >> 3) & 1) << 0;
				b2 = ((cc1 >> 7) & 1) << 1;
				b1 = ((cc2 >> 3) & 1) << 2;
				b0 = ((cc2 >> 7) & 1) << 3;
				break;
			case 2:	//second group of four bits, offset = 2
				b3 = ((cc1 >> 2) & 1) << 0;
				b2 = ((cc1 >> 6) & 1) << 1;
				b1 = ((cc2 >> 2) & 1) << 2;
				b0 = ((cc2 >> 6) & 1) << 3;
				break;
			case 1:	//third group of four bits, offset = 1
				b3 = ((cc1 >> 1) & 1) << 0;
				b2 = ((cc1 >> 5) & 1) << 1;
				b1 = ((cc2 >> 1) & 1) << 2;
				b0 = ((cc2 >> 5) & 1) << 3;
				break;
			case 0:	//fourth group of four bits, offset = 0
				b3 = ((cc1 >> 0) & 1) << 0;
				b2 = ((cc1 >> 4) & 1) << 1;
				b1 = ((cc2 >> 0) & 1) << 2;
				b0 = ((cc2 >> 4) & 1) << 3;
				break;
			}
		c = b0 | b1 | b2 | b3;
		if(c)
			s[i] = P[c | attrib];
		}
	s += p;
	}
}
